package demo1;

import demo2.Cell;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class gameJFrame extends JFrame implements ActionListener {

    // 初始化菜单
    JMenuBar jMenuBar = new JMenuBar();
    JMenu functionMenu = new JMenu("菜单");
    JMenu aboutMenu = new JMenu("关于我们");

    // 创建条目对象
    JMenuItem rule = new JMenuItem("俄罗斯规则");
    JMenuItem keyButton = new JMenuItem("按键说明");
    JMenuItem replay = new JMenuItem("重新游戏");
    JMenuItem author = new JMenuItem("制作人");

    private Tetris panel;
    private SoundPlayer soundPlayer;

    public gameJFrame() {
        init();
        initJMenubar();

        this.setVisible(true);
        startGame();
    }

    private void init() {
        this.setSize(810, 945);
        this.setTitle("俄罗斯方块");
        panel = new Tetris();
        this.add(panel);

        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setResizable(false);

        soundPlayer = new SoundPlayer();
    }

    private void initJMenubar() {
        functionMenu.add(replay);
        functionMenu.add(keyButton);
        functionMenu.add(rule);

        aboutMenu.add(author);

        // 给条目绑定事件
        rule.addActionListener(this);
        keyButton.addActionListener(this);
        replay.addActionListener(this);
        author.addActionListener(this);

        // 添加至菜单中
        jMenuBar.add(functionMenu);
        jMenuBar.add(aboutMenu);

        this.setJMenuBar(jMenuBar);
    }

    private void startGame() {
        soundPlayer.playSound(Statement.SP_GROUND);
        panel.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        if (obj==rule){
            if (Desktop.isDesktopSupported()) {
                Desktop desktop = Desktop.getDesktop();try {
                    desktop.browse(new URI("https://tetris.huijiwiki.com/wiki/%E6%B8%B8%E6%88%8F%E6%A6%82%E8%A7%88"));}
                catch (IOException | URISyntaxException a) {
                    a.printStackTrace();
                }
            }
        } else if (obj == replay) {
            //表示游戏重新开始
            Statement.game_state = Statement.PLAYING;
            Statement.wall = new Cell[18][9];
            Statement.currentOne = Tetromino.randomOne();
            Statement.nextOne = Tetromino.randomOne();
            Statement.totalScore = 0;
            Statement.totalLine = 0;
        } else if (obj==keyButton) {
            JDialog jDialog=new JDialog();
            jDialog.setTitle("按键说明");
            JLabel jLabel=new JLabel(new ImageIcon("images/key.jpg"));
            jLabel.setBounds(0,0,407,619);
            jDialog.getContentPane().add(jLabel);
            jDialog.setSize(500,690);
            jDialog.setAlwaysOnTop(true);
            jDialog.setLocationRelativeTo(null);
            jDialog.setModal(true);
            jDialog.setVisible(true);
        } else if (obj == author) {
            JDialog jDialog=new JDialog();
            jDialog.setTitle("制作人介绍");
            JLabel jLabel=new JLabel(new ImageIcon("images/Author.jpg"));
            jLabel.setBounds(-5,-5,1800,950);
            jDialog.getContentPane().add(jLabel);
            jDialog.setSize(1800,950);
            jDialog.setAlwaysOnTop(true);
            jDialog.setLocationRelativeTo(null);
            jDialog.setVisible(true);
        }

    }
}
